﻿-- Компилирует все процедури
CREATE PROCEDURE [dbo].[USPTOOLS_Encrypted]
	@ObjName [varchar](2000)
WITH EXECUTE AS CALLER
AS
set nocount on
declare @objNewName varchar(1000), @Inc int
declare @OrigEnc nvarchar(4000), 
@ModEnc nvarchar(4000), 
@ModModEnc nvarchar(4000), 
@NewEnc nvarchar(4000)



declare @tmpEncryptedTable table (
IdentitySeed int identity(1,1) not null,
EncryptedText nvarchar(4000) not null)
declare @tmpEmptyUnencryptedTable table (
IdentitySeed int identity(1,1) not null,
EncryptedText nvarchar(4000) not null)
declare @tmpEmptyEncryptedTable table (
IdentitySeed int identity(1,1) not null,
EncryptedText nvarchar(4000) not null)
declare @tmpDecryptedTable table (
IdentitySeed int identity(1,1) not null,
DecryptedText nvarchar(4000) not null)
insert into @tmpEncryptedTable (EncryptedText)
select sc.ctext from dbo.syscomments sc where sc.encrypted = 1 and sc.id = (select so.id 
from dbo.sysobjects so inner join dbo.sysusers su on (su.uid = so.uid) 
where su.name = 'dbo' and so.name=@ObjName) order by sc.colid asc


declare @ModEncInc int
set @ModEncInc = 0
declare DecryptorCursor cursor fast_forward for select EncryptedText from 
@tmpEncryptedTable order by IdentitySeed asc
open DecryptorCursor
fetch next from DecryptorCursor into @OrigEnc
while @@fetch_status = 0
begin
if(@ModEncInc = 0)
begin
insert into @tmpEmptyUnencryptedTable 
select 'CREATE PROCEDURE [dbo].['+@ObjName+'] with encryption as ' + replicate('-', 3800)
end
else
insert into @tmpEmptyUnencryptedTable select replicate(N'-', len(@OrigEnc))
set @ModEncInc = @ModEncInc + 1
fetch next from DecryptorCursor into @OrigEnc
end
close DecryptorCursor
deallocate DecryptorCursor

declare @str nvarchar(4000) 
set @str='ALTER PROCEDURE [dbo].['+@ObjName+'] with encryption as '+replicate('-', 3800)
begin transaction DecryptObject
execute(@str)
insert into @tmpEmptyEncryptedTable (EncryptedText)
select sc.ctext from dbo.syscomments sc where sc.encrypted = 1 and sc.id =
 (select so.id 
from dbo.sysobjects so 
inner join dbo.sysusers su on (su.uid = so.uid) 
where su.name = 'dbo' and so.name=@ObjName) order by sc.colid
rollback transaction DecryptObject


declare @EncA nvarchar(4000), @EncB nvarchar(4000), @EncC nvarchar(4000)
declare @idseed int
declare DecryptorCursor cursor fast_forward for select a.IdentitySeed, a.EncryptedText 
from @tmpEncryptedTable a 
order by a.IdentitySeed asc 
open DecryptorCursor
fetch next from DecryptorCursor into @idseed, @EncA
while @@fetch_status = 0
begin
select @EncB = EncryptedText from @tmpEmptyUnencryptedTable where IdentitySeed = @idseed
select @EncC = EncryptedText from @tmpEmptyEncryptedTable where IdentitySeed = @idseed
set @Inc=1
set @NewEnc = replicate(N'A', (datalength(@EncA)/2))
while @Inc <= (datalength(@NewEnc)/2)
begin
set @NewEnc = stuff(@NewEnc, @Inc, 1, nchar(unicode(substring(@EncA, @Inc, 1)) ^
(unicode(substring(@EncB, @Inc, 1)) ^ unicode(substring(@EncC, @Inc, 1)))))
set @Inc = @Inc + 1
end
insert into @tmpDecryptedTable select @NewEnc
fetch next from DecryptorCursor into @idseed, @EncA
end
close DecryptorCursor
deallocate DecryptorCursor
select DecryptedText from @tmpDecryptedTable order by IdentitySeed asc
set nocount off


